package com.zwt.util.sms;


import com.zwt.util.http.HttpRequest;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;



public class TSMS {
	private static String url="http://sdk999in.eucp.b2m.cn:8080/sdkproxy/%s";
	private static String cdkey="9SDK-EMY-0999-SFVLP";
	private static String password="873503";
//	private static String url="http://hprpt2.eucp.b2m.cn:8080/sdkproxy/%s";
//	private final static String cdkey="8SDK-EMY-6699-SETMT";
//	private final static String password="420241";
	
	private static Logger logger = LoggerFactory.getLogger(TSMS.class);
	/**
	 * 专用通道签名配置
	 */
	public static final int MAIN_SMS = 1;
	/**
	 * 数据中心签名配置
	 */
	public static final int DATA_SMS = 2;
	/**
	 * 营销通道签名配置
	 */
	public static final int MARKET_SMS = 3;
	
	private static void init(int type){
		/*if(type == 1){//数据中心  目前有两种签名，分别对应不同的短信通道账号
			url=ConfigProperty.getProperty("main.sms.url")+"";
			cdkey=ConfigProperty.getProperty("main.sms.cdkey")+"";
			password=ConfigProperty.getProperty("main.sms.pass")+"";
		}else if(type == 2){
			url=ConfigProperty.getProperty("data.sms.url")+"";
			cdkey=ConfigProperty.getProperty("data.sms.cdkey")+"";
			password=ConfigProperty.getProperty("data.sms.pass")+"";
		}else{
			url=ConfigProperty.getProperty("sms.url")+"";
			cdkey=ConfigProperty.getProperty("sms.cdkey")+"";
			password=ConfigProperty.getProperty("sms.pass")+"";
		}*/
	}
	/**
	 * 此处走的是营销通道
	 * @param phone
	 * @param message
	 * @return
	 */
	public static int sendsms(String phone,String message){
		return sendsms(TSMS.MARKET_SMS,phone,message);
	}
	/**
	 * 发送短信返回状态码 1:成功
	 * @param phone
	 * @param message
	 * @param type 类型   1：票补专用通道  2：数据中心发送短信 3：票补账号营销短信
	 * @return
	 */
	public static int sendsms(int type,String phone,String message){
		//init(type);
		logger.info("发送短信，接收手机号码："+phone+"---短信内容："+message);
		Map<String, String> map=new HashMap<String, String>();
		map.put("cdkey", cdkey);
		map.put("password", password);
		map.put("phone", phone);
		map.put("message", message);
		map.put("addserial", "100823");
		map.put("smspriority", "5");
		url=String.format(url, "sendsms.action");
		String xml= HttpRequest.postUrl(url, map);
		if(!StringUtils.isEmpty(xml)){
			Document doc;
			try {
				doc = (Document) DocumentHelper.parseText(xml);
				Element response = doc.getRootElement();  
				String code=response.element("error").getText();
				logger.info("发送短信返回状态："+code);
				if("0".equals(code)){
					return 1;
				}else {
					return Integer.parseInt(code);
				}
			} catch (Exception e) {
				logger.error("发送短信出错："+e.toString());
				e.printStackTrace();
			}  
		}
		return 0;
	}
	/**
	 * 发送短信返回状态码 1:成功
	 * @param
	 * @param
	 * @param
	 * @return
	 */
	public static int getMess(){
//		logger.info("发送短信，接收手机号码："+phone+"---短信内容："+message);
		init(1);
		Map<String, String> map=new HashMap<String, String>();
		map.put("cdkey", cdkey);
		map.put("password", password);
		url=String.format(url, "getmo.action");
		String xml= HttpRequest.postUrl(url, map);
		System.out.println(xml);
		if(!StringUtils.isEmpty(xml)){
			Document doc;
			try {
				doc = (Document)DocumentHelper.parseText(xml);
			    Element response = doc.getRootElement();
			    String code=response.element("error").getText();
			    logger.info("发送短信返回状态："+code);
			    if("0".equals(code)){
			    	return 1;
			    }else {
					return Integer.parseInt(code);
				}
			} catch (Exception e) {
				logger.error("发送短信出错："+e.toString());
				e.printStackTrace();
			}  
		}
		return 0;
	}
	public static void regist(){
		Map<String, String> map=new HashMap<String, String>();
		map.put("cdkey", cdkey);
		map.put("password", password);
		url=String.format(url, "regist.action");
		try {
			String xml= HttpRequest.postUrl(url, map);
			System.out.println(xml);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args){
//		regist();
//		int result= sendsms("18210138204",
//				"【用户中心】事实上身上发生的收货人姓名s：撒啊啊：18210138204");
		int result= sendsms(TSMS.MARKET_SMS,"13161617140",
				"【推影科技】尊敬的推影用户您好，影片《妖猫传》新鲜出炉，票补多多，申请限量先到先得，请赶快登录推影平台获取您的票补福利！");
		System.out.println(result);
//		while(true){
//			getMess();
//			try {
//				Thread.sleep(1500);
//			} catch (InterruptedException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		}
		
	}

}
